<template>
  <div class="container">
    <HeaderImage></HeaderImage>
    <div class="list">
      <div
        v-for="(item, index) in articles"
        :key="index"
        class="list-item"
        @click="toDetail(item.id)"
      >
        <div style="font-size: 32px">{{ item.title }}</div>
        <div style="font-size: 22px; color: grey">{{ item.description }}</div>
        <div style="font-size: 14px; margin-top: 10px; color: grey">{{ new Date(item.created_at).toLocaleString() }}</div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { getArticles } from '../../api/index'
import type { Article } from '../../api/index'
import HeaderImage from '@/components/HeaderImage.vue'

const articles = ref([] as Article[])
const router = useRouter()

onMounted(() => {
  getArticles().then((res) => {
    articles.value = res.data.articles
  })
})

const toDetail = (id: number) => {
  router.push({ name: 'article', params: { id: id } })
}
</script>

<style scoped>
.container {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.list {
  margin: 30px;
  max-width: 960px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.list-item {
  width: 100%;
  margin: 10px;
  padding: 15px;
  cursor: pointer;
}
</style>
